package com.huitone.gddw.controller.monitoring.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.ItemKeyDef;
import com.huitone.gddw.dao.entity.ObjItemProperty;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.monitoring.base.ItemKeyDefDTO;
import com.huitone.gddw.service.monitoring.base.IItemKeyDefService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 对象子项属性管理
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="itemKeyDef")
public class ItemKeyDefController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(ItemKeyDefController.class);
	
	@Autowired
	private IItemKeyDefService itemKeyDefService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<ItemKeyDefDTO> query(QueryPage<ItemKeyDef> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询属性列表";
		QueryPage<ItemKeyDefDTO> result = null;
		try {
			page.setParams(getParams(request));
			result = itemKeyDefService.selectPage(page);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return result;
		} catch (Exception e) {
			LOGGER.error("查询属性信息出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return result;
		}
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String objId = request.getParameter("objId");
		String itemId = request.getParameter("itemId");
		String objType = request.getParameter("objType");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("objId", objId);
		params.put("itemId", itemId);
		params.put("objType", objType);
		return params;
	}
	
	/**
	 * 获取指定子项下的所有属性信息
	 * @param itemId 子项ID
	 * @return List<ObjItemProperty>
	 */
	@ResponseBody
	@RequestMapping(value="getByItemId")
	public List<ObjItemProperty> getByItemId(String itemId) {
		try {
			return itemKeyDefService.getByItemId(itemId);
		} catch (Exception e) {
			LOGGER.error("获取指定子项下的所有属性信息过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 准备修改页面的信息
	 * @param keyId 键ID
	 * @return ItemKeyDefDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public ItemKeyDefDTO toUpdate(String keyId) {
		try {
			return itemKeyDefService.findOne(keyId);
		} catch (Exception e) {
			LOGGER.error("监控配置-属性管理-准备修改页面信息时出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 保存
	 * @param entity ItemKeyDef
	 * @param request
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(ItemKeyDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加属性信息";
		try {
			itemKeyDefService.save(entity);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("保存属性信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "保存失败！");
		}
	}
	
	/**
	 * 修改
	 * @param entity ItemKeyDef
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(ItemKeyDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改属性信息";
		try {
			ItemKeyDef iemKeyDef = itemKeyDefService.getById(entity.getKeyId());
			iemKeyDef.setKey(entity.getKey());
			iemKeyDef.setKeyCn(entity.getKeyCn());
			iemKeyDef.setItemId(entity.getItemId());
			iemKeyDef.setSeq(entity.getSeq());
			iemKeyDef.setValueType(entity.getValueType());
			iemKeyDef.setPrecision(entity.getPrecision());
			iemKeyDef.setUnits(entity.getUnits());
			iemKeyDef.setUpdatetime(entity.getUpdatetime());
			iemKeyDef.setHistory(entity.getHistory());
			iemKeyDef.setTrends(entity.getTrends());
			iemKeyDef.setTrendsdim(entity.getTrendsdim());
			iemKeyDef.setTrendsdimUnit(entity.getTrendsdimUnit());
			iemKeyDef.setRemark(entity.getRemark());
			itemKeyDefService.update(iemKeyDef);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "修改成功！");
		} catch (Exception e) {
			LOGGER.error("修改属性信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "修改失败！");
		}
	}
	
	/**
	 * 删除
	 * @param keyId 键ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String keyId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除属性信息";
		try {
			itemKeyDefService.delete(keyId);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "删除成功！");
		} catch (Exception e) {
			LOGGER.error("删除属性信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "删除失败！");
		}
	}
	
}
